package com.online.shopping.activemq;

import com.alibaba.fastjson.JSON;
import com.online.shopping.pojo.TbItem;
import com.online.shopping.service.ItemSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import java.util.List;

@Component
public class ItemListener {

	@Autowired
	private ItemSearchService itemSearchService;

	@JmsListener(destination = JmsConfig.TOPIC_SOLR, containerFactory = "jmsListenerContainerTopic")
	public void onItemSearch(Message message) {
		TextMessage textMessage=(TextMessage)message;
		try {
			String text = textMessage.getText();     // json字符串
			System.out.println("监听到消息:"+text);  // text: json数组

			List<TbItem> itemList = JSON.parseArray(text, TbItem.class);
			itemSearchService.importList(itemList);
			System.out.println("导入到solr索引库");

		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
